/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.bean;

import me.panpf.sketch.Sketch;
import me.panpf.sketch.cache.BitmapPool;
import me.panpf.sketch.cache.DiskCache;
import me.panpf.sketch.cache.MemoryCache;
import me.panpf.sketch.sample.event.Events;
import me.panpf.sketch.sample.item.SettingsItemProvider;
import me.panpf.sketch.sample.util.FileUtils;
import ohos.agp.components.BaseItemProvider;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.Revocable;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import org.greenrobot.eventbus.EventBus;

/**
 * CacheInfoMenu
 */
public class CacheInfoMenu extends InfoMenu {
    private static final int EVENT_ID_CLEAN_CACHE = 1;
    private Context mContext;
    private String type;

    public CacheInfoMenu(Context context, String type, String title) {
        super(title);
        this.mContext = context;
        this.type = type;
        this.title = title;
    }

    @Override
    public String getInfo() {
        switch (type) {
            case "Memory": {
                MemoryCache memoryCache = Sketch.with(mContext).getConfiguration().getMemoryCache();
                String usedSizeFormat = FileUtils.formetFileSize(memoryCache.getSize());
                String maxSizeFormat = FileUtils.formetFileSize(memoryCache.getMaxSize());
                return usedSizeFormat + "/" + maxSizeFormat;
            }
            case "Disk": {
                DiskCache diskCache = Sketch.with(mContext).getConfiguration().getDiskCache();
                String usedSizeFormat = FileUtils.formetFileSize(diskCache.getSize());
                String maxSizeFormat = FileUtils.formetFileSize( diskCache.getMaxSize());
                return usedSizeFormat + "/" + maxSizeFormat;
            }
            case "BitmapPool": {
                BitmapPool bitmapPool = Sketch.with(mContext).getConfiguration().getBitmapPool();
                String usedSizeFormat = FileUtils.formetFileSize(bitmapPool.getSize());
                String maxSizeFormat = FileUtils.formetFileSize(bitmapPool.getMaxSize());
                return usedSizeFormat + "/" + maxSizeFormat;
            }
            default: return "Unknown Type";
        }
    }

    @Override
    public void onClick(BaseItemProvider provider) {
        switch (type) {
            case "Memory": {
                Sketch.with(mContext).getConfiguration().getMemoryCache().clear();
                notifyDataSetItemChanged(provider);
                EventBus.getDefault().post(new Events.CacheCleanEvent());
                break;
            }
            case "Disk": {
                String dispatcherName = "parallelTaskDispatcher";
                TaskDispatcher parallelTaskDispatcher = mContext.createParallelTaskDispatcher(dispatcherName, TaskPriority.DEFAULT);
                parallelTaskDispatcher.asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        if (mContext != null) {
                            Sketch.with(mContext).getConfiguration().getDiskCache().clear();
                        }

                        new CleanCacheHandler(EventRunner.getMainEventRunner()).sendEvent(InnerEvent.get(EVENT_ID_CLEAN_CACHE, provider));
                    }
                });
                break;
            }
            case "BitmapPool": {
                Sketch.with(mContext).getConfiguration().getBitmapPool().clear();
                notifyDataSetItemChanged(provider);
                EventBus.getDefault().post(new Events.CacheCleanEvent());
                break;
            }
            default:
                break;
        }
    }

    private void notifyDataSetItemChanged(BaseItemProvider provider) {
        if (provider != null) {
            final int startPos = 1;
            final int countItems = 3;
            provider.notifyDataSetItemRangeChanged(startPos, countItems);
        }
    }

    private class CleanCacheHandler extends EventHandler {
        public CleanCacheHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            if ((event != null) && (event.eventId == EVENT_ID_CLEAN_CACHE)) {
                if (mContext != null) {
                    BaseItemProvider provider = (BaseItemProvider) event.object;
                    notifyDataSetItemChanged(provider);
                    EventBus.getDefault().post(new Events.CacheCleanEvent());
                }
            }
        }
    }
}
